From: ktoutire@chopin.ecs.umass.edu (Kiran K Toutireddy)
Newsgroups: comp.lang.c++
Subject: INITIALIZATION of a MEMBER CLASS of a CLASS
Date: 17 Apr 1996 16:54:27 GMT
Organization: University of Massachusetts, Amherst
Message-ID: <4l37o3$iug@risky.ecs.umass.edu>
NNTP-Posting-Host: chopin-gw.ecs.umass.edu
X-Newsreader: TIN [version 1.2 PL2]
Hi,
I have a question related to initializing the member classes inside a class data structure. The following example should explain the question.
I have a class called Engine, which can be initialized to Gas engine,Deisel Engine, Electric Engine, etc. by default, it is electric engine.(default constructor).
I have a totally unrelated class(no inheritances...) called Car.
Now this Car class has a member
Engine car_engine;
Since all the cars I consider have only gas engines( no EVs considered :-),
I want to initialize the car_engine member to be Gas engine by default, instead of the default electric engine. How do I do that?
the following is some pseudo code of the above example
class Engine{
nuts..bolts..pipes..rods...;
public:
Engine(){ make this an electric engine,....;}
Engine(int Gas){ make it a gas engine..}
Engine(float Deisel){ make it a deisel engine;}
};
class Car{
Engine car_engine;
public:
Car(){ how do i initialize the car_engine to be of type Gas here?;}
};
I tried initializing the car_engine by saying Engine car_engine(int Gas), but it gives a warning saying that ANSI c++ forbids such declarations.